﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SAE_Jam
{
    public class Player
    {
        private const float idleDelay = 3000.0f;
        private const float startSpeed = 0.5f;

        public SpriteAnimation animation;

        private float timer = 0;
        private float speed = 0.5f;
        private bool isIdle = false;
        private BoxCollider collider;
        private Vector2 tempPos = Vector2.Zero;

        public void LoadContent(ContentManager content)
        {
            animation = new SpriteAnimation("MasterChief", content.Load<Texture2D>("MasterChief"), content.RootDirectory + "/MasterChief.xml");
            animation.AnimationDelay = 100;
            animation.Position = new Vector2(100, 100);
            this.collider = new BoxCollider((int)this.animation.Position.X, (int)this.animation.Position.Y, this.animation.Source.Width, this.animation.Source.Height);
        }
        public void Update(GameTime gameTime)
        {
            Move(gameTime);
            UpdateColliderPosition();
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            animation.Draw(spriteBatch);
        }

        private void Move(GameTime gameTime)
        {
            if (InputManager.KeyDown(Keys.W))
            {
                timer = 0;
                animation.color = Color.White;
                tempPos = animation.Position;
                tempPos = new Vector2(tempPos.X, tempPos.Y -= speed * gameTime.ElapsedGameTime.Milliseconds);
                animation.Position = tempPos;
                animation.PlayAnimation("Gun1_Walk");
                animation.Update(gameTime);
            }

            if (InputManager.KeyDown(Keys.S))
            {
                timer = 0;
                animation.color = Color.White;
                tempPos = animation.Position;
                tempPos = new Vector2(tempPos.X, tempPos.Y += speed * gameTime.ElapsedGameTime.Milliseconds);
                animation.Position = tempPos;
                animation.PlayAnimation("Gun1_Walk");
                animation.Update(gameTime);
            }

            if (InputManager.KeyDown(Keys.A))
            {
                timer = 0;
                animation.color = Color.White;
                tempPos = animation.Position;
                tempPos = new Vector2(tempPos.X -= speed * gameTime.ElapsedGameTime.Milliseconds, tempPos.Y);
                animation.Position = tempPos;
                animation.PlayAnimation("Gun1_Walk");
                animation.Update(gameTime);
            }

            if (InputManager.KeyDown(Keys.D))
            {
                timer = 0;
                animation.color = Color.White;
                tempPos = animation.Position;
                tempPos = new Vector2(tempPos.X += speed * gameTime.ElapsedGameTime.Milliseconds, tempPos.Y);
                animation.Position = tempPos;
                animation.PlayAnimation("Gun2_Walk");
                animation.Update(gameTime);
            }

            else
            {
                timer += gameTime.ElapsedGameTime.Milliseconds;
                if (timer >= idleDelay)
                {
                    animation.color = Color.Red;
                }
            }
        }

        private void UpdateColliderPosition()
        {
            this.collider.X = (int)this.animation.Position.X;
            this.collider.Y = (int)this.animation.Position.Y;
            this.collider.Width = this.animation.Source.Width;
            this.collider.Height = this.animation.Source.Height;
        }

        private void OnCollision(BoxCollider other)
        {
            if (other.X > this.collider.X && InputManager.KeyDown(Keys.D))
            {
                this.speed = 0;
            }
        }
    }
}
